<template>
  <div class="main">
    <el-container class="main-content">
      <!-- 侧边栏1 -->
      <el-aside :width="isCollapse ? '60px' : '210px'">
        <Nav-Menu :width="sliderWidth" :collapse="isCollapse" ref="Menu" />
      </el-aside>
      <el-container class="page">
        <el-header class="page-header">
          <NaV-Header :isFold="isCollapse" @foldChange="handleFoldChange" />
        </el-header>
        <!-- 主体 -->
        <el-main class="page-content">
          <router-view class="page-info" v-slot="{ Component }">
            <transition
              leave-active-class="leave-active "
              enter-active-class="enter-active "
              mode="out-in"
              appear
            >
              <keep-alive>
                <component :is="Component" />
              </keep-alive>
            </transition>
          </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts">
import NavMenu from '@/components/NavMenu/NavMenu.vue';
import NaVHeader from '@/components/NaVHeader/NaVHeader.vue';

import { localCache } from '@/utils';
import { ref, defineComponent, computed } from 'vue';

const COLLAPSE_CACHE_HISTORY = 'navCollapseHistory';

export default defineComponent({
  setup() {
    const isCollapse = ref(
      localCache.getCache(COLLAPSE_CACHE_HISTORY) || false
    );

    const Menu = ref(null);

    const handleFoldChange = () => {
      isCollapse.value = !isCollapse.value;
      localCache.setCache(COLLAPSE_CACHE_HISTORY, isCollapse.value);
    };

    const sliderWidth = computed(() => {
      return isCollapse.value ? '60px' : '210px';
    });

    return {
      isCollapse,
      Menu,
      handleFoldChange,
      sliderWidth,
    };
  },

  components: {
    NavMenu,
    NaVHeader,
  },
});
</script>

<style lang="less" scoped>
.main {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.main-content,
.page {
  height: 100%;
}

.page-content {
  height: calc(100% - 48px);
  position: relative;

  .page-info {
    background-color: #fff;
    border-radius: 5px;
    height: 100%;
  }
}

.el-header,
.el-footer {
  display: flex;
  color: #333;
  text-align: center;
  align-items: center;
}

.el-header {
  height: 48px !important;
}

.el-aside {
  overflow-x: hidden;
  overflow-y: auto;
  line-height: 200px;
  text-align: left;
  cursor: pointer;
  background-color: #001529;
  transition: width 0.3s linear;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE 10+ */

  &::-webkit-scrollbar {
    display: none;
  }
}

.el-main {
  color: #333;
  text-align: center;
  background-color: #f0f2f5;
}

.enter-active {
  animation: 0.8s ease jackInTheBox;
}

.leave-active {
  animation: 0.3s ease lightSpeedOutRight;
}
</style>
